package com.prs.crm.action.hr;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Train;
import com.prs.crm.domain.hr.TrainItem;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.hr.TrainItemService;
import com.prs.crm.service.hr.TrainService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class TrainAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Train train;
	private TrainService trainService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private Person person;
	private PersonService personService;
	private TrainItemService trainItemService;

	private File upload;
	private String uploadFileName;
	private String deptIds;
	private List<TrainItem> trainItems;

	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;

	private Code code;
	private CodeService codeService;
	private String Encode;
	private Collection<CodeCommon> codelist;

	@Action("listTrainPerson")
	public String listTrainPerson() {
		return SUCCESS;
	}

	@Action("listTrainPersonByDept")
	public String listTrainPersonByDept() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Person.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.in("dept", this.getPersonService().convertToDept(deptIds)));
		this.setPagination(this.getPersonService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listTrain")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Train.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTrainService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listMyTrain")
	public String listMyTrain() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Train.class);
		criteria.add(Restrictions.eq("trainPerson", AppContext
				.getAccountContext().currentUser()));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTrainService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editTrain")
	public String edit() {
		trainItems = this.getTrainItemService().getAllTrainItem();
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"培训");
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveTrain", results = {
			@Result(name = "success", type = "redirect", location = "listTrainPerson"),
			@Result(name = "error", type = "chain", location = "editTrain") })
	public String save() {
		if (train.getTitle() == null || "".equals(train.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (train.getItem() == null) {
			addActionError("培训项目不能为空");
			return ERROR;
		}
		if (train.getPerid() == null || "".equals(train.getPerid().trim())) {
			addActionError("课时不能为空");
			return ERROR;
		}
		if(train.getTraincode()==null||"".equals(train.getTraincode().trim())){
			addActionError("编码不能为空");
			return ERROR;
		}
		if ("n".equals(Encode)) {
			int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码",
					"培训", train.getTraincode());
			if (EncodeRplice != 0) {
				this.addActionError("编码重复!请重新输入");
				return ERROR;
			}
		}
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(train.getTraincode());
			codes.setEntityName(this.getClass().getName());
			codes.setCodeType("手动编码");
			codes.setCodeMolde("培训");
			this.getCodeService().save(codes);
			train.setCode(codes);
			train.setTrainPerson(person);
			train.setState(Constrains.TASK_AUDITING);
			this.getTrainService().saveTrains(train, upload, uploadFileName);
		} else {
			CodeCommon Vob = this.getCodeCommonService().getVobject(
					codeCommon.getId());
			Code codes = new Code();
			codes.setEntityName(this.getClass().getName());
			codes.setCodeValue(train.getTraincode());
			Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
			this.codeCommonService.saveOrUpdate(Vob);
			codes.setCodeType(Vob.getCodePlan());
			codes.setCodeMolde("培训");
			this.getCodeService().save(codes);
			train.setCode(codes);
			train.setTrainPerson(person);
			train.setState(Constrains.TASK_AUDITING);
			this.getTrainService().saveTrains(train, upload, uploadFileName);
		}
		this.setPrompt("提交人员培训成功");

		return SUCCESS;
	}

	@Action(value = "removeTrain", results = { @Result(name = "success", type = "redirect", location = "listTrain") })
	public String remove() {
		this.getTrainService().batchRemove(ids);
		return SUCCESS;
	}

	@Action("viewTrain")
	public String view() {
		return SUCCESS;
	}

	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public TrainService getTrainService() {
		return trainService;
	}

	public void setTrainService(TrainService trainService) {
		this.trainService = trainService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public void setTrainItems(List<TrainItem> trainItems) {
		this.trainItems = trainItems;
	}

	public List<TrainItem> getTrainItems() {
		return trainItems;
	}

	public void setTrainItemService(TrainItemService trainItemService) {
		this.trainItemService = trainItemService;
	}

	public TrainItemService getTrainItemService() {
		return trainItemService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getDeptIds() {
		return deptIds;
	}

	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

}
